package com.ayou.eggguard.farm.controller;

import com.ayou.common.utils.Result;
import com.ayou.eggguard.farm.pojo.Keeper;
import com.ayou.eggguard.farm.service.KeeperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/keeper")
public class KeeperController {

    @Autowired
    private KeeperService keeperService;

    @PostMapping("/addKeeper")
    public Result addKeeper(@RequestParam String farmName,@RequestBody Keeper keeper){
        Keeper k=keeperService.getKeeperByUsername(keeper.getUsername());
        if(k!=null){
            return Result.error("账号已存在");
        }else{
            keeperService.addKeeper(farmName,keeper);
            return Result.success();
        }
    }

    @GetMapping("/getKeepers")
    public Result<List<Keeper>> getKeepers(String farmName){
        List<Keeper> keepers=keeperService.getKeepersByFarmName(farmName);
        return Result.success(keepers);
    }

    @GetMapping("/deleteKeeper")
    public Result deleteKeeper(String username){
        keeperService.deleteKeeperByUsername(username);
        return Result.success();
    }

    @GetMapping("/resetKeeperPwd")
    public Result resetKeeperPwd(String username){
        keeperService.resetKeeperPwd(username);
        return Result.success();
    }

}
